
package model.test;


import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedList;

/**
 *
 * @author FELIPE & NICOLAS
 */
public class SubSection {
    
    
    private String name;
    private String descTxt;
    private LinkedList<Question> questions;
    
    
    //constructor 1
    public SubSection(String name,String descTxt){
        
        this.name=name;
        this.descTxt =descTxt;
        questions = new LinkedList<Question>();
        
    }
    
    ////constructor 2 (from a string readed from a txt file, which has been pased by the SECTION constructor)
    public SubSection (String inputString){
        
        questions = new LinkedList<Question>();
        
        String[] readResultSubSection = inputString.split("%");//readResultSubSetion is an array that represents each subsection, which contain info and in each element a question
        
        //data about the subsection
        this.name = readResultSubSection[0];     
        this.descTxt = readResultSubSection[1];
        
        //create its questions passing the rest of the elements in the String and add them to this subSection
        for (int k=2;k<readResultSubSection.length;k++){
            
            //check what type of question it is (only split to get first parameter, the real spliting is done inside the question constructor)
            String type = readResultSubSection[k].split("~")[0];
            
            Question question;
            
            if (type.equals("MCQ")){
                
                question = new MCQ(readResultSubSection[k]);
                questions.add(question);
            }
            else if (type.equals("FIBQ")){
                
                question = new FIBQ(readResultSubSection[k]);
                questions.add(question);
            }
            else if (type.equals("MFIBQ")){
                question = new MFIBQ(readResultSubSection[k]);
                questions.add(question);
            }
        }
    }
    
    
    
    //method to add a question to this subsection
    public void addQuestion(Question question){
        
        questions.add(question);
    }
    
    //method to remove a question from this subsection
    public void removeQuestion(Question question){
        
        questions.remove(question); 
        
    }
    
    
    
    
    //method to replace a question indicating the position in the LinkedList
    public void replaceQuestion(Question newQuestion,int position){
        
        questions.set(position, newQuestion);
                
    }
    

    //toString method
    public String toString(){
        
        String result=name+"%"+descTxt;
        
        Iterator<Question> iter = questions.iterator();//create an iterator to navigate through the elements of the collection
        
        while ( iter.hasNext() ){
            result=result+("%")+(iter.next().toString() );
        }
        
        return result;
    }
    
    
    //GETTERS AND SETTERS

    public String getDescTxt() {
        return descTxt;
    }

    public void setDescTxt(String descTxt) {
        this.descTxt = descTxt;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public LinkedList<Question> getQuestions() {
        return questions;
    }

    public void setQuestions(LinkedList<Question> questions) {
        this.questions = questions;
    }
    
    
    
    
    
    //FOR TESTING PURPOSE
    public static void main(String[] args) {
	
         //create a subsection
	 SubSection tempSubSect= new SubSection("SubSection1","hellobd");
         System.out.println(tempSubSect);
         
         //create 2 questions and add them to the subsection
         ArrayList<String> possAnswers = new ArrayList<String>();
         possAnswers.add("optionA");
         possAnswers.add("optionB");
         possAnswers.add("optionC");
         MCQ temp = new MCQ("What is?",10,5,possAnswers,2,"jaja.jpg");
         MCQ temp2 = new MCQ("Who is?",10,5,possAnswers,2,"jaja.jpg");
         
         tempSubSect.addQuestion(temp);
         tempSubSect.addQuestion(temp2);
         System.out.println(tempSubSect);
         

    }
    
}
